﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.NTD
{
    public partial class ListUngVien : System.Web.UI.Page
    {
        public int trang { get; set; }
        public int nganhngheid { get; set; }
        public int trangthaiid { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["NTD"] == null)
            {
                Response.Redirect("/nha-tuyen-dung.html");
            }
            if (Request.QueryString["cate"] != null)
            {
                nganhngheid = int.Parse(Request.QueryString["cate"]);
            }
            else
            {
                nganhngheid = -1;
            }
            if (Request.QueryString["status"] != null)
            {
                trangthaiid = int.Parse(Request.QueryString["status"]);
            }
            else
            {
                trangthaiid = -1;
            }
            if (Request.QueryString["page"] != null)
            {
                trang = int.Parse(Request.QueryString["page"]);
            }
            else
            {
                trang = 1;
            }
            if (!IsPostBack)
            {
                LoadNganhNghe();
                
                LoadData(nganhngheid, trangthaiid);
            }
        }
        public void LoadNganhNghe()
        {
            DAL.UserTuyenDung user = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            ListItem item = new ListItem();
            item.Text = "--Chọn ngành nghề--";
            item.Selected = true;
            item.Value = "-1";
            droTinTuyenDung.DataSource = BLL.TuyenDung.DangTuyenHelper.List_TinTuyenDungAll(user.UserId);
            droTinTuyenDung.DataTextField = "ChucDanh";
            droTinTuyenDung.DataValueField = "TuyenDungId";
            droTinTuyenDung.DataBind();
            droTinTuyenDung.Items.Add(item);
        } 
        public void LoadData(int Tuyendungid, int tinhtrang)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            DAL.UserTuyenDung user = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            bool All = false;
            bool Trangthai = false;
            if(tinhtrang==-1)
            {
                All = true;
            }
            if(tinhtrang==2)
            {
                Trangthai = true;
            }
            int page = 1;
            droTinTuyenDung.SelectedValue = nganhngheid.ToString();
            droTrangThai.SelectedValue = trangthaiid.ToString();
            if (Request.QueryString["page"] != null)
            {
                page = int.Parse(Request.QueryString["page"]);
            }
            rptListUV.DataSource = BLL.TuyenDung.DangTuyenHelper.List_UngVienCuaToi(Tuyendungid, Trangthai, page, 10, All);
            rptListUV.DataBind();
            int Count = BLL.TuyenDung.DangTuyenHelper.CountList_List_UngVienCuaToi(Tuyendungid, Trangthai, All);
            int TotalPage = Count / 10 + 1;
            if (Count < 1)
            {
                lbtPreview.Visible = false;
                lbtNext.Visible = false;
            }
            if (page == 1)
            {
                lbtPreview.Visible = false;
            }
            if (page == TotalPage)
            {
                lbtNext.Visible = false;
            }
            for (int i = page - 4; i < page + 4; i++)
            {
                if (i < 1)
                    continue;
                if (i > TotalPage)
                    continue;
                if (i == page && Count > 0)
                    ltlPaging.Text += "<li><a class='current' href='" + path + "?cate=" + nganhngheid + "&status=" + trangthaiid + "&page=" + i + "'>" + i + "</a></li>";
                else
                    ltlPaging.Text += "<li><a href='" + path + "?cate=" + nganhngheid + "&status=" + trangthaiid + "&page=" + i + "'>" + i + "</a></li>";
            }
        }

        protected void lbtPreview_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + nganhngheid + "&status=" + trangthaiid + "&page=" + (trang - 1));
        }

        protected void lbtNext_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + nganhngheid + "&status=" + trangthaiid + "&page=" + (trang + 1));
        }
        protected void btSearch_Click(object sender, EventArgs e)
        {
            string path = HttpContext.Current.Request.Url.AbsolutePath;
            Response.Redirect(path + "?cate=" + droTinTuyenDung.SelectedValue + "&status=" + droTrangThai.SelectedValue + "&page=1");
        }
       

        protected void rptListUV_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName=="Xem")
            {
                int Hosoid = int.Parse(e.CommandArgument.ToString().Split('-')[0]);
                int tuyendungid = int.Parse(e.CommandArgument.ToString().Split('-')[1]);
                BLL.TuyenDung.DangTuyenHelper.UpDataTrangThaiDaDocDonTuyenDung(tuyendungid,Hosoid);
                Response.Redirect("/nguoi-dung/"+Hosoid+"xem-ho-so.html");
            }
            if (e.CommandName == "Email")
            {
                int Hosoid = int.Parse(e.CommandArgument.ToString());
                Response.Redirect("/nha-tuyen-dung/"+Hosoid+"confrim-ung-vien.html");
            }
        }
        
        
    }
}